@extends('develop.layoutEdit')

@section('css')
    <link rel="stylesheet" type="text/css" href="/static/libs/uploadify/uploadify.css?v=20170509">
    <style>
        .uploadify {
            float: left;
            margin-right: 10px;
        }
        .layer-go-back{
            display:none;
        }
    </style>
@endsection

@section('title')
    <li class="cur">
        <span>微信设置</span>
    </li>
@endsection

@section('content')

    <div class="form-group">
        <label class="col-sm-3 control-label" for="gh_access_type"><span class="red pr5">*&nbsp;</span>公众号接入模式：</label>
        <div class="col-sm-5">
            <div class="layui-form">
                <input type="radio" name="gh_access_type"  value="1" title="常规接入" lay-filter="nocheck" @if( !isset($configure['gh_access_type']) || $configure['gh_access_type'] == 1 )checked @endif>
                <input type="radio" name="gh_access_type"  value="2"  title="授权接入" lay-filter="check" @if( isset($configure['gh_access_type']) && $configure['gh_access_type'] == 2 )checked @endif>
            </div>
        </div>
    </div>

    @if( !isset($configure['gh_access_type']) || $configure['gh_access_type'] == 1 )

        <div class="form-group access-type" id="check_appid{{$configure['gh_access_type'] or ''}}" style="display: none">
            <label class="col-sm-3 control-label" for="component_appid"><span class="red pr5">*&nbsp;</span>开放平台appid：</label>
            <div class="col-sm-5">
                <input id="component_appid" class="form-control" type="text" placeholder="开放平台appid" name="component_appid" value="{{ $configure['component_appid'] or ''}}">
            </div>
        </div>

        <div class="form-group access-type" id="check_appsecret{{$configure['gh_access_type'] or ''}}" style="display: none">
            <label class="col-sm-3 control-label" for="component_appsecret"><span class="red pr5">*&nbsp;</span>开放平台appsecret：</label>
            <div class="col-sm-5">
                <input id="component_appsecret" class="form-control" type="text" placeholder="开放平台appsecret" name="component_appsecret" value="{{ $configure['component_appsecret'] or ''}}">
            </div>
        </div>

        <div class="form-group access-type" id="check_token{{$configure['gh_access_type'] or ''}}" style="display: none">
            <label class="col-sm-3 control-label" for="component_token"><span class="red pr5">*&nbsp;</span>开放平台token：</label>
            <div class="col-sm-5">
                <input id="component_token" class="form-control" type="text" placeholder="开放平台token" name="component_token" value="{{ $configure['component_token'] or ''}}">
            </div>
        </div>

        <div class="form-group access-type" id="check_encodingaeskey{{$configure['gh_access_type'] or ''}}" style="display: none">
            <label class="col-sm-3 control-label" for="component_encodingaeskey"><span class="red pr5">*&nbsp;</span>开放平台解密密钥：</label>
            <div class="col-sm-5">
                <input id="component_encodingaeskey" class="form-control" type="text" placeholder="开放平台解密密钥" name="component_encodingaeskey" value="{{ $configure['component_encodingaeskey'] or ''}}">
            </div>
        </div>

    @endif

    @if( isset($configure['gh_access_type']) && $configure['gh_access_type'] == 2 )

    <div class="form-group access-type" id="check_appid{{$configure['gh_access_type'] or ''}}">
        <label class="col-sm-3 control-label" for="component_appid"><span class="red pr5">*&nbsp;</span>开放平台appid：</label>
        <div class="col-sm-5">
            <input id="component_appid" class="form-control" type="text" placeholder="开放平台appid" name="component_appid" value="{{ $configure['component_appid'] or ''}}">
        </div>
    </div>

    <div class="form-group access-type" id="check_appsecret{{$configure['gh_access_type'] or ''}}">
        <label class="col-sm-3 control-label" for="component_appsecret"><span class="red pr5">*&nbsp;</span>开放平台appsecret：</label>
        <div class="col-sm-5">
            <input id="component_appsecret" class="form-control" type="text" placeholder="开放平台appsecret" name="component_appsecret" value="{{ $configure['component_appsecret'] or ''}}">
        </div>
    </div>

    <div class="form-group access-type" id="check_token{{$configure['gh_access_type'] or ''}}">
        <label class="col-sm-3 control-label" for="component_token"><span class="red pr5">*&nbsp;</span>开放平台token：</label>
        <div class="col-sm-5">
            <input id="component_token" class="form-control" type="text" placeholder="开放平台token" name="component_token" value="{{ $configure['component_token'] or ''}}">
        </div>
    </div>

    <div class="form-group access-type" id="check_encodingaeskey{{$configure['gh_access_type'] or ''}}">
        <label class="col-sm-3 control-label" for="component_encodingaeskey"><span class="red pr5">*&nbsp;</span>开放平台解密密钥：</label>
        <div class="col-sm-5">
            <input id="component_encodingaeskey" class="form-control" type="text" placeholder="开放平台解密密钥" name="component_encodingaeskey" value="{{ $configure['component_encodingaeskey'] or ''}}">
        </div>
    </div>

    @endif

    <div class="form-group">
        <label class="col-sm-3 control-label" for="wechat_name"><span class="red pr5">*&nbsp;</span>公众号名称：</label>
        <div class="col-sm-5">
            <input id="wechat_name" class="form-control" type="text" placeholder="公众号名称" name="wechat_name" value="{{ $configure['wechat_name'] or ''}}">
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-3 control-label" for="wechat_type"><span class="red pr5">*&nbsp;</span>公众号类型：</label>
        <div class="col-sm-9">
            <div class="layui-form">
                <input type="radio" name="wechat_type"  value="1"  title="服务号" @if( !isset($configure['wechat_type']) || $configure['wechat_type'] == 1 ) checked @endif>
                <input type="radio" name="wechat_type"  value="0" title="订阅号" @if( isset($configure['wechat_type']) && $configure['wechat_type'] == 0 ) checked @endif>
            </div>
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-3 control-label" for="wechat_auth_flg"><span class="red pr5">*&nbsp;</span>公众号认证：</label>
        <div class="col-sm-9">
            <div class="layui-form">
                <input type="radio" name="wechat_auth_flg"  value="0"  title="未认证" @if( !isset($configure['wechat_auth_flg']) || $configure['wechat_auth_flg'] == 0 ) checked @endif>
                <input type="radio" name="wechat_auth_flg"  value="1"  title="已认证" @if( isset($configure['wechat_auth_flg']) && $configure['wechat_auth_flg'] == 1 ) checked @endif>
            </div>
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-3 control-label" for="wechat_originalid"><span class="red pr5">*&nbsp;</span>公众号原始ID：</label>
        <div class="col-sm-5">
            <input id="wechat_originalid" class="form-control" type="text" placeholder="公众号原始ID" name="wechat_originalid" value="{{ $configure['wechat_originalid'] or ''}}">
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-3 control-label" for="wechat_wxno"><span class="red pr5">*&nbsp;</span>微信号：</label>
        <div class="col-sm-5">
            <input id="wechat_wxno" class="form-control" type="text" placeholder="微信号" name="wechat_wxno" value="{{ $configure['wechat_wxno'] or ''}}">
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-3 control-label" for="wechat_token"><span class="red pr5">*&nbsp;</span>微信TOKEN：</label>
        <div class="col-sm-5">
            <input id="wechat_token" class="form-control" type="text" placeholder="微信TOKEN" name="wechat_token" value="{{ $configure['wechat_token'] or ''}}">
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-3 control-label" for="wechat_encodingaeskey"><span class="red pr5">*&nbsp;</span>消息加解密密钥：</label>
        <div class="col-sm-5">
            <input id="wechat_encodingaeskey" class="form-control" type="text" placeholder="消息加解密密钥" name="wechat_encodingaeskey" value="{{ $configure['wechat_encodingaeskey'] or ''}}">
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-3 control-label" for="wechat_appid"><span class="red pr5">*&nbsp;</span>微信APPID：</label>
        <div class="col-sm-5">
            <input id="wechat_appid" class="form-control" type="text" placeholder="微信APPID" name="wechat_appid" value="{{ $configure['wechat_appid'] or ''}}">
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-3 control-label" for="wechat_appsecret"><span class="red pr5">*&nbsp;</span>微信APPSECRET：</label>
        <div class="col-sm-5">
            <input id="wechat_appsecret" class="form-control" type="text" placeholder="微信APPSECRET" name="wechat_appsecret" value="{{ $configure['wechat_appsecret'] or ''}}">
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-3 control-label"><span class="red pr5">*&nbsp;</span>公众号二维码：</label>
        <div class="col-sm-9 form-inline">
            <input type="file"  id="update_logo_1">
            <button type="button" class="btn btn-default" onclick="plugin.option_photo(1);">从图片库选择</button>
            <div id="logo_img_box_1" style="margin-top: 5px;">
            @if( isset( $configure['wechat_orcode'] ) )
                <img style="width: 200px;height: 100px;" src="{{ $configure['wechat_orcode'] or ''}}">
                <a href="javascript: void(0);" id="del_log_img_1">删除</a>
            @endif
            </div>
            <input type="hidden" name="wechat_orcode" id="wechat_pic_1" value="{{ $configure['wechat_orcode'] or ''}}">
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-3 control-label">一键关注图标：</label>
        <div class="col-sm-9">
            <input type="file"  id="update_logo_2"/>
            <button type="button" class="btn btn-default" onclick="plugin.option_photo(2);">从图片库选择</button>
            <button class="btn btn-default" onclick="wechat_subscribe.choose();" type="button">已有图标</button>
            <div id="logo_img_box_2" style="margin-top: 5px;">
            @if( isset( $configure['wechat_attenetion_logo'] ) )
                <img  id="pic_link" style="width: 200px;height: 100px;" src="{{ $configure['wechat_attenetion_logo'] or ''}}">
                <a href="javascript: void(0);" id="del_log_img_2">删除</a>
            @endif
            </div>
            <input type="hidden" name="wechat_attenetion_logo" id="wechat_attenetion_logo_2" value="{{ $configure['wechat_attenetion_logo'] or ''}}">
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-3 control-label" for="wechat_subscribe_url"><span class="red pr5">*&nbsp;</span>一键关注网址：</label>
        <div class="col-sm-6">
            <input id="wechat_subscribe_url" class="form-control" type="text" placeholder="一键关注网址" name="wechat_subscribe_url" value="{{ $configure['wechat_subscribe_url'] or ''}}">
            <span class="help-block">由于微信一键关注代码被腾讯禁用，所以我们用二次跳转的方法做一个引导页</span>
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-3 control-label"></label>
        <div class="col-sm-9">
            <input type="hidden" name="function_id" id="function_id" value="{{ $function_id or ''}}">
            <input type="button"  value="保存" onclick="func.edit();" class="btn btn-success" >
        </div>
    </div>

@endsection

@section('js')
    <script type="text/javascript" src="/static/libs/uploadify/jquery.uploadify.min.js"></script>
    <script type="text/javascript" src="/static/ebsig/jquery.photo.min.js?v=20151219"></script>
    <script>

        layui.use(['form'], function(){
            var form = layui.form();
            var gh_access_type = {{$configure['gh_access_type'] or 1}};
            form.on('radio(check)', function(data){
                if ( this.checked ) {
                    $('#check_appid'+gh_access_type).show();
                    $('#check_appsecret'+gh_access_type).show();
                    $('#check_token'+gh_access_type).show();
                    $('#check_encodingaeskey'+gh_access_type).show();
                }
            });
            form.on('radio(nocheck)', function(data){
                if ( this.checked ) {
                    $('#check_appid'+gh_access_type).hide();
                    $('#check_appsecret'+gh_access_type).hide();
                    $('#check_token'+gh_access_type).hide();
                    $('#check_encodingaeskey'+gh_access_type).hide();
                }
            });
        });

        var func = {

            //保存
            edit:function(){

                //表单数据
                var dt = E.getFormValues("edit-form");
                var configure ={ };
                configure.function_id = $('#function_id').val();
                configure.gh_access_type    = dt.gh_access_type;

                if ( dt.gh_access_type == 1 ) {
                    dt.component_appid = '';
                    dt.component_appsecret = '';
                    dt.component_token = '';
                    dt.component_encodingaeskey = '';
                }

                configure.component_appid    = dt.component_appid;
                configure.component_appsecret    = dt.component_appsecret;
                configure.component_token    = dt.component_token;
                configure.component_encodingaeskey    = dt.component_encodingaeskey;
                configure.wechat_name                   = dt.wechat_name;
                configure.wechat_type = dt.wechat_type;
                configure.wechat_auth_flg = dt.wechat_auth_flg;
                configure.wechat_originalid   = dt.wechat_originalid;
                configure.wechat_wxno    = dt.wechat_wxno;
                configure.wechat_token    = dt.wechat_token;
                configure.wechat_encodingaeskey    = dt.wechat_encodingaeskey;
                configure.wechat_appid    = dt.wechat_appid;
                configure.wechat_appsecret    = dt.wechat_appsecret;
                configure.wechat_orcode    = dt.wechat_orcode;
                configure.wechat_attenetion_logo    = dt.wechat_attenetion_logo;
                configure.wechat_subscribe_url    = dt.wechat_subscribe_url;

                var msg ='';

                if ( E.isEmpty(dt.gh_access_type) ) {
                    msg+='请选择公众号接入模式</br>';
                }

                if ( !E.isEmpty(dt.gh_access_type) && dt.gh_access_type == 2 ) {

                    if ( E.isEmpty(dt.component_appid) ) {
                        msg+='请填写开放平台appid</br>';
                    }

                    if ( E.isEmpty(dt.component_token) ) {
                        msg+='请填写开放平台token</br>';
                    }

                    if ( E.isEmpty(dt.component_encodingaeskey) ) {
                        msg+='请填写开放平台解密密钥</br>';
                    }

                }

                if ( E.isEmpty(dt.wechat_name) ) {
                    msg+='请填写公众号名称</br>';
                }

                if ( E.isEmpty(dt.wechat_type) ) {
                    msg+=' 请选择公众号类型</br>';
                }


                if ( E.isEmpty(dt.wechat_auth_flg) ) {
                    msg+=' 请选择公众号认证</br>';
                }

                if ( E.isEmpty(dt.wechat_originalid) ) {
                    msg+=' 请填写公众号原始ID</br>';
                }

                if ( E.isEmpty(dt.wechat_wxno) ) {
                    msg+='  请填写微信号</br>';
                }

                if ( E.isEmpty(dt.wechat_token) ) {
                    msg+='  请填写微信TOKEN</br>';
                }

                if ( E.isEmpty(dt.wechat_encodingaeskey) ) {
                    msg+='  请填写消息加解密密钥</br>';
                }

                if ( E.isEmpty(dt.wechat_appid) ) {
                    msg+='  请填写微信APPID</br>';
                }

                if ( E.isEmpty(dt.wechat_appsecret) ) {
                    msg+='  请填写微信APPSECRET</br>';
                }

                if ( E.isEmpty(dt.wechat_orcode) ) {
                    msg+='  请上传公众号二维码</br>';
                }

                if ( E.isEmpty(dt.wechat_subscribe_url) ) {
                    msg+='  请填写 一键关注网址</br>';
                }


                if(msg){
                    layer.alert(msg,{icon:2});
                    return false;
                }

                layer.confirm("您确认保存该设置吗？",{icon:3,offset:'50px'}, function ( index ){
                    layer.close( index );
                    E.ajax( {
                        type:'get',
                        url: '/lar/develop/function/store/'+function_id,
                        data: configure,
                        dataType:'json',
                        // 返回值判断
                        success: function ( obj ){

                            if ( obj.code == 200 ) {
                                layer.alert( obj.message , { icon: 1,time : 1500 } ) ;
                            } else {
                                layer.alert( obj.message , { icon: 2 } ) ;
                            }
                        }
                    } ) ;
                });
            }

        };

        var plugin = {

            index : 0 ,

            //选择图片插件
            option_photo: function( index  ) {

                plugin.index = index  ;

                layer.open({
                    title: '选择图片',
                    type: 2,
                    scrollbar: false,
                    area: ['100%', '100%'],
                    content: '/postsystem/app/plugin/photoPlugin.pscript?operFlg=1'
                });

            },

            photo: function ( data ) {
                $('#logo_img_box_'+ plugin.index ).html('<img style="width:200px;height:100px;" src="' + data.link + '"> <a href="javascript: void(0);"  id="del_log_img_' + plugin.index + '">删除</a>').show();
                $('#wechat_pic_'+ plugin.index).val( data.link );
            }

        } ;

        var wechat_subscribe = {

            pic_num: 7,

            choose: function() {

                var  html = '<table class="table table-bordered" style="height: 400px;width: 800px;">';
                html += '<tbody>';
                html += '<tr>';
                html += '<th width="120">操作</th>';
                html += '<th width="300">一键关注图标</th>';
                html += '</tr>';
                for(var i = 1; i <= wechat_subscribe.pic_num; i++) {
                    var pic = '/postsystem/docroot/templates/images/attention/attention' + i + '.gif';
                    html += '<tr>';
                    html += '<td><a href="javascript:void(0)" onclick="wechat_subscribe.select(\'' + pic + '\')" >选择</a></td>';
                    html += '<td><img src="' + pic + '" /></td>';
                    html += '</tr>';
                }
                html += '</tbody>';
                html += '</table>';

                layer.open({
                    content: html,
                    title: '一键关注图标列表',
                    area: ['800px'],
                    btn:false
                });

            },

            select: function( pic_link ) {
                layer.closeAll();
                $('#logo_img_box_2').html('<img style="width: 200px" src="' + pic_link + '"> <a href="javascript: void(0);" id="del_log_img_2">删除</a>').show();
                $('#wechat_attenetion_logo_2').val(pic_link);
            }

        };

        $(document).on('click', 'a[id^="del_log_img_"]', function() {
            var key = $(this).attr('id').replace('del_log_img_', '');
            $('#logo_img_box_' + key).html('').hide();
            $('#wechat_attenetion_logo_' + key).val('') ;
        });

        $(window).ready(function() {
            //上传控件
            $("input[id^='update_logo_']").uploadify({
                buttonText: '<div>上传图片</div>',
                height: 35,
                swf  : '/static/libs/uploadify/uploadify.swf',
                uploader: '/external/upload.php?t=photo&group_id=1',
                width: 120,
                onUploadSuccess: function (file, data) {

                    data = eval('(' + data + ')');
                    var key = $(this)[0]['settings']['id'].replace('update_logo_', "");
                    var url = data.url;

                    $('#logo_img_box_' + key).html('<img style="width:200px;height:100px;" src="' + url + '"> <a href="javascript: void(0);" id="del_log_img_' + key + '">删除</a>').show();
                    $('#wechat_pic_' + key).val(url);
                }
            });

            //上传控件
            $("button[id^='pic_']").ebsigPhoto({
                title: '图片',
                multiple: false,
                success: function (photo_data, id) {
                    var url = photo_data.link;
                    var key = id.replace('pic_', "");
                    $('#logo_img_box_' + key).html('<img style="width:200px;height:100px;" src="' + url + '"> <a href="javascript: void(0);" id="del_log_img_' + key + '">删除</a>').show();
                    var url_data = url.replace('<!--{$ebsig_comm.website.domain_name}-->', '');
                    $('#' + key).val(url_data);
                }
            });
        });

    </script>
@endsection